package cn.kxx.interceptor;

import cn.kxx.entity.Result;
import cn.kxx.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(value= BusinessException.class)
    @ResponseBody
    public Result processBusinessExcetion(BusinessException e){
        log.error("=====>>>发生了自定义异常信息："+e.getMessage());
        return new Result(false, e.getMessage());
    }

    /**
     * 处理系统异常
     */
    @ExceptionHandler(value= Exception.class)
    @ResponseBody

    public Result processExcetion(Exception e){
        log.info("=================>>>>>发生系统异常:"+e.getMessage());
        System.out.println(e.getMessage());
        //如果是权限异常直接抛出，axios捕获后处理
        if (e instanceof AccessDeniedException){
            throw new AccessDeniedException("不允许访问");
        }
        return new Result(false,"未知异常");
    }
}